﻿using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CallCenter.Data;
using CallCenter.Data.BizUtilities;
using System.Configuration;
using System.Data;
using CallCenter.Security;
using CallCenter.Data.Models;
using CallCenter.Data.Misc;

using System.IO;

namespace CallCenter.Web.Controllers
{
    public class UploadController : BaseController
    {
        // GET: Upload
        public ActionResult Index()
        {
            return View();
        }


        public JsonResult GetSalesUploadfiles(int SalesLeadId)
        {
            var uploadfiles = this.Bridge.SalesLeadUtility.GetSalesUploadfiles(SalesLeadId);
            return Json(uploadfiles, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult DeleteUploadedFiles(int SalesLeadId , int id)
        {
            
            this.Bridge.SalesLeadUtility.DeleteUploadFiles(SalesLeadId, id);
            var returnMessage = "Success";
            return Json(returnMessage, JsonRequestBehavior.AllowGet);
            
        }


        [HttpPost]
        public ActionResult Submit(IEnumerable<HttpPostedFileBase> files)
        {
            if (files != null)
            {
                TempData["UploadedFiles"] = GetFileInfo(files);
            }

            return RedirectToRoute("Demo", new { section = "upload", example = "result" });
        }


        public JsonResult Save(IEnumerable<HttpPostedFileBase> files, int SalesID)
        {
            if (files != null)
            {
                foreach (var file in files)
                {
                    //var fileName = Path.GetFileName(file.FileName);
                    Guid guid = Guid.NewGuid();
                    string FileName = guid.ToString() + file.FileName;
                    string FileURL = ConfigurationManager.AppSettings["baseUrl"].ToString() + "Documents/SalesLead/" + FileName.Replace('-', '_');
                    var physicalPath = Path.Combine(Server.MapPath("~/Documents/SalesLead"), FileName.Replace('-', '_'));
                    file.SaveAs(physicalPath);
                    this.Bridge.SalesLeadUtility.InsertToSalesUpload(SalesID, FileURL, file.FileName.Replace('-', '_'));
                }
            }

            var uploadfiles = this.Bridge.SalesLeadUtility.GetSalesUploadfiles(SalesID);
            return Json(uploadfiles, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Remove(string[] fileNames)
        {
            // The parameter of the Remove action must be called "fileNames"

            if (fileNames != null)
            {
                foreach (var fullName in fileNames)
                {
                    var fileName = Path.GetFileName(fullName);
                    var physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);

                    // TODO: Verify user permissions

                    if (System.IO.File.Exists(physicalPath))
                    {
                        // The files are not actually removed in this demo
                        // System.IO.File.Delete(physicalPath);
                    }
                }
            }

            // Return an empty string to signify success
            return Content("");
        }

        private IEnumerable<string> GetFileInfo(IEnumerable<HttpPostedFileBase> files)
        {
            return
                from a in files
                where a != null
                select string.Format("{0} ({1} bytes)", Path.GetFileName(a.FileName), a.ContentLength);
        }
    }
}